package swu.cx.recyclerviewtest

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView

class MyAdapter:RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
    class MyViewHolder(item:View):RecyclerView.ViewHolder(item){
            val img = item.findViewById<ImageView>(R.id.icon)
            val des = item.findViewById<TextView>(R.id.description)
            init {
                img.setOnClickListener {
                    Toast.makeText(item.context,"你选择了:${item.tag}", Toast.LENGTH_SHORT).show()

                }
            }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
            val view = LayoutInflater.from(parent.context).inflate(R.layout.item_linearlayout,parent,false)
            return MyViewHolder(view)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val data = Repository.instance.datas[position]
        holder.img.setImageResource(data.img)
        holder.des.text = data.description
        holder.itemView.tag = data.name
    }

    override fun getItemCount(): Int {
            return Repository.instance.datas.size
    }
}